package com.rich.dmr.ui.activity;

import android.annotation.SuppressLint;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.rich.dmr.R;
import com.rich.dmr.adapter.DialingGridAdapter;
import com.rich.dmr.utils.SoundPoolUtils;

import java.util.ArrayList;
import java.util.List;

import rich.dmr.DmrManager;

public class DialingActivity extends AppCompatActivity implements View.OnClickListener, View.OnLongClickListener {
    private DmrManager dmrManager;
    private EditText etCall;

    @SuppressLint("WrongConstant")
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dialing);
        dmrManager = (DmrManager) getSystemService("dmrservice");
        RecyclerView recyclerView = findViewById(R.id.dial_rv);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 3);
        recyclerView.setLayoutManager(gridLayoutManager);
        gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                if (position == 10) {
                    return 2;
                }
                return 1;
            }
        });
        DialingGridAdapter adapter = new DialingGridAdapter(DialingActivity.this, getListNumName());
        recyclerView.setAdapter(adapter);
        adapter.setOnClickListener(new DialingGridAdapter.OnItemClickListener() {
            @Override
            public void ItemClickListener(View view, int position) {
                if (0 <= position && position < 9) {
                    etCall.append((position + 1) + "");
                } else if (position == 9) {
                    etCall.append(0 + "");
                } else {
                    String contact = etCall.getText().toString();
                    if (!contact.equals("") && dmrManager != null) {
                        int mTxContactVal = Integer.valueOf(contact.trim());
                        if (mTxContactVal <= 0 || (mTxContactVal > 16776415 && mTxContactVal < 16777200) || mTxContactVal > 16777215) {
                            Toast.makeText(DialingActivity.this, "invalid", Toast.LENGTH_SHORT).show();
                            return;
                        }

                        dmrManager.setTransmitContact(mTxContactVal);
                        if (mTxContactVal <= 16776415) {
                            dmrManager.setContactType(0);
                            dmrManager.flush();
                        } else {
                            dmrManager.setContactType(2);
                            dmrManager.flush();
                        }
                        SoundPoolUtils.getInstance(DialingActivity.this).playSound(false);
                        finish();
                    }
                }
            }

            @Override
            public void ItemLongClickListener(View view, int position) {

            }
        });

        etCall = findViewById(R.id.et_call);
        LinearLayout ltDel = findViewById(R.id.ll_del);
        ltDel.setOnClickListener(this);
        ltDel.setOnLongClickListener(this);
    }

    private List<String> getListNumName() {
        List<String> dataList = new ArrayList<>();
        TypedArray typedArray = getResources().obtainTypedArray(R.array.dial_num_type);
        if (typedArray != null) {
            for (int i = 0; i < typedArray.length(); i++) {
                dataList.add(typedArray.getString(i));
            }
        }
        typedArray.recycle();
        return dataList;
    }

    @Override
    public void onClick(View view) {
        int position = etCall.getSelectionStart();
        if (etCall.getText().toString().equals("")) {
            finish();
        } else {
            etCall.getText().delete(position - 1, position);
        }
    }

    @Override
    public boolean onLongClick(View view) {
        etCall.getText().clear();
        return true;
    }
}
